package uiLayer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.ProductCtr;
import controllerLayer.SaleCtr;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import modelLayer.Customer;
import modelLayer.Employee;
import modelLayer.SaleLinesItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class TimberG extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JTextField textField_1;
	private JButton btnNewButton_1;
	private JLabel lblCustomersCpr;
	private JLabel lblEmployeesCpr;
	private JTextField textField_2;
	private JButton btnNewButton_2;
	private Employee employee;
	private Customer customer;
	private JLabel lblTimber;
	private JButton btnNewButton_3;
	private EmployeeCtr ectr;
	private CustomerCtr cctr;
	private ProductCtr pctr;
	private static InputParser inp;
	private JTextPane textPane;
	private JTextField textField_3;
	private JLabel lblQuantity;
	private JLabel lblId;
	private ArrayList<SaleLinesItem> shoppingCart;	
	private SaleCtr sctr;
	private int id;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmPrint;
	private JMenuItem mntmPrintDeliveryNote;
	private JMenuItem mntmPrintDispatchNote;
	private JMenu mnReset;
	private JMenuItem mntmEmployee;
	private JMenuItem mntmCustomer;
	private JMenuItem mntmShoppingCart;
	private JMenuItem mntmAll;
		

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					TimberG frame = new TimberG();
					inp.load();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TimberG() {
		ectr = new EmployeeCtr();
		cctr = new CustomerCtr();
		pctr = new ProductCtr();
		sctr = new SaleCtr();
		inp = InputParser.getInstance();
		shoppingCart = new ArrayList<SaleLinesItem>();	
		setTitle("TIMBER");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 519);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnNewMenu = new JMenu("Print");
		menuBar.add(mnNewMenu);
		
		mntmPrint = new JMenuItem("Print Order");
		mntmPrint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					printOrder();
				}
						catch(NullPointerException npe)
						{
							textPane.setText("create offer at first");
						}
			}
		});
		mnNewMenu.add(mntmPrint);
		
		mntmPrintDeliveryNote = new JMenuItem("Print Delivery Note");
		mntmPrintDeliveryNote.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					printDeliveryNote();
				}
						catch(NullPointerException npe)
						{
							textPane.setText("accept offer at first");
						}
			}
		});
		mnNewMenu.add(mntmPrintDeliveryNote);
		
		mntmPrintDispatchNote = new JMenuItem("Print Dispatch Note");
		mntmPrintDispatchNote.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					printDispatchNote();
				}
						catch(NullPointerException npe)
						{
							textPane.setText("accept offer at first");
						}
			}
		});
		mnNewMenu.add(mntmPrintDispatchNote);
		
		mnReset = new JMenu("Reset");
		menuBar.add(mnReset);
		
		mntmEmployee = new JMenuItem("Employee");
		mntmEmployee.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				employee = null;
				textField_1.setText("");
				textField_1.setEnabled(true);
				textPane.setText("Employee has been cleared.");	
			}
		});
		mnReset.add(mntmEmployee);
		
		mntmCustomer = new JMenuItem("Customer");
		mntmCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				customer = null;
				textField_2.setText("");
				textField_2.setEnabled(true);
				textPane.setText("Customer has been cleared.");				}
		});
		mnReset.add(mntmCustomer);
		
		mntmShoppingCart = new JMenuItem("Shopping cart");
		mntmShoppingCart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shoppingCart = null;
				textPane.setText("Shopping cart has been cleared.");	
			}
		});
		mnReset.add(mntmShoppingCart);
		
		mntmAll = new JMenuItem("All");
		mntmAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				TimberG frame = new TimberG();
				frame.setVisible(true);
			}
		});
		mnReset.add(mntmAll);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setVisible(true);
		
		textField = new JTextField();
		textField.setBounds(107, 105, 154, 20);
		textField.setColumns(10);
		
		btnNewButton = new JButton("Enter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String ID = textField.getText();
				double quantity = inp.parseDouble(textField_3.getText());												
				try{
				textPane.setText(pctr.getSaleLinesItem(ID).toString()+ "................" + quantity + pctr.getSaleLinesItem(ID).getMeasure());
				pctr.countQuantity(ID, quantity);
				shoppingCart.add(pctr.getSaleLinesItem(ID));
				if (pctr.isRunningOut(pctr.getSaleLinesItem(ID).getProduct()))textPane.setText(textPane.getText()+"\n Product is running out.");
				}
				
				catch(NullPointerException npe ){
					textPane.setText("Item doesn't exist");
				}
				
					catch (IllegalArgumentException iae){
						textPane.setText(iae.getMessage());
					}

			}
		});
		btnNewButton.setBounds(422, 104, 76, 23);
		contentPane.setLayout(null);
		contentPane.add(textField);
		contentPane.add(btnNewButton);
		
		textField_1 = new JTextField();
		textField_1.setBounds(439, 12, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		btnNewButton_1 = new JButton("Enter");
		btnNewButton_1.addActionListener(new ActionListener() {
			

			

			public void actionPerformed(ActionEvent arg0) { 
			String empId = textField_1.getText();
			try{
				employee = ectr.getEmployee(empId);
				textPane.setText(employee.toString());
				textField_1.setEnabled(false);
			}
			catch (NullPointerException npe){
				textPane.setText("Employee doesn't exist. Try again.");
			}
			
			
			
			
			}
		});
		btnNewButton_1.setBounds(535, 11, 89, 23);
		contentPane.add(btnNewButton_1);
		
		lblCustomersCpr = new JLabel("Customer's CPR:");
		lblCustomersCpr.setBounds(326, 45, 101, 14);
		contentPane.add(lblCustomersCpr);
		
		lblEmployeesCpr = new JLabel("Employee's CPR:");
		lblEmployeesCpr.setBounds(326, 15, 103, 14);
		contentPane.add(lblEmployeesCpr);
		
		textField_2 = new JTextField();
		textField_2.setBounds(439, 47, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		btnNewButton_2 = new JButton("Enter");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String custId = textField_2.getText();
				try{
					customer = cctr.getCustomer(custId);
					textPane.setText(customer.toString());
					textField_2.setEnabled(false); 
					}
					catch (NullPointerException npe){
						textPane.setText("Customer doesn't exist. Try again.");
					}
				
			}
		});
		btnNewButton_2.setBounds(535, 41, 89, 23);
		contentPane.add(btnNewButton_2);
		
		lblTimber = new JLabel("TIMBER");
		lblTimber.setFont(new Font("Verdana", Font.PLAIN, 28));
		lblTimber.setBounds(86, 12, 160, 79);
		contentPane.add(lblTimber);
		
		btnNewButton_3 = new JButton("Create Offer");
		btnNewButton_3.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent arg0) {
				
				try{
					id = sctr.createOffer(customer,employee,shoppingCart);
				}
				catch(NullPointerException npe){
				}
				textPane.setText("ID of your offer is: " + id + "\n" +sctr.findSale(id).printOffer(id));
			}});
		btnNewButton_3.setBounds(508, 427, 116, 23);
		contentPane.add(btnNewButton_3);
		
		textPane = new JTextPane();
		textPane.setBounds(86, 148, 412, 302);
		contentPane.add(textPane);
		
		textField_3 = new JTextField();
		textField_3.setBounds(326, 105, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		lblQuantity = new JLabel("Quantity:");
		lblQuantity.setBounds(269, 108, 59, 14);
		contentPane.add(lblQuantity);
		
		lblId = new JLabel("ID:");
		lblId.setBounds(87, 108, 23, 14);
		contentPane.add(lblId);
	}

	public void printOrder(){
		pctr.sellProduct(employee.getId(), customer.getId(), shoppingCart);
		textPane.setText("Order:"+sctr.findSale(id).printOffer(id));
	}
	
	private void printDispatchNote(){
		textPane.setText("Dispatch Note"+sctr.findSale(id).printDispatchNote(id));
	}
	
	private void printDeliveryNote(){
		textPane.setText("Delivery Note"+sctr.findSale(id).printDeliveryNote(id));
	}
}

